package com.xh.audit.controller.auditdocument;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.auditdocument.AuditDocumentTemplate;
import com.xh.audit.service.AuditDocumentTemplateService;
import com.xh.audit.vo.webform.AuditDocumentTemplateQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 审计文书模版 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/audit-document-template")
@RequiredArgsConstructor
@Validated
public class AuditDocumentTemplateController {
    private final AuditDocumentTemplateService auditDocumentTemplateService;

    @PostMapping("/save")
    public R<AuditDocumentTemplate> save(@Validated
                                         AuditDocumentTemplate tosave) {
        boolean saveFlag = auditDocumentTemplateService.documentTemplateSave(tosave);

        if (saveFlag) {
            return R.success(auditDocumentTemplateService.getById(tosave.getId()));
        }
        return R.failed("保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<AuditDocumentTemplate>> query(@RequestBody
                                                @Validated AuditDocumentTemplateQuery queryPage) {
        Page<AuditDocumentTemplate> page1 = auditDocumentTemplateService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<AuditDocumentTemplate> info(@PathVariable
                                         @Validated
                                         @NotNull(message = "id不能空")
                                         Integer id) {

        AuditDocumentTemplate byId = auditDocumentTemplateService.getById(id);
        return R.success(byId);
    }

    /**
     * List<Integer> 集合验证 必须在类上也加 @Validated
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<AuditDocumentTemplate> del(@RequestBody
                                        @Validated
                                        @NotEmpty(message = "ids 集合不能空")
                                        List<Integer> ids) {

        auditDocumentTemplateService.removeByIdsWithWps(ids);
        
        return R.success("删除成功");
    }

}
